package yawning.mew.bear;

import yawning.mew.sim.SimulationEvent;

public class SavageDefenseEvent extends SimulationEvent {
	BearThreatSimulator mState;

	SavageDefenseEvent(BearThreatSimulator state) {
		super(state.mSimEngine);
		mState = state;
	}

	@Override
	public void onExpire() {
//		if (mState.mIsLogging) {
//			if (mCurrentShield > 0) {
//				mState.mLog.log("Savage Defense Expiration: Remaining: %d\n", Math.round(mCurrentShield));
//			} else {
//				mState.mLog.log("Savage Defense Expiration\n");
//			}
//		}
		mCurrentShield = 0;
	}

	void onTrigger() {
		onTrigger(false);
	}

	void onTrigger(boolean forceTrigger) {
		if (forceTrigger || mState.mSimEngine.getRandom().nextBoolean(mState.mModel.pSavageDefense)) {
			int shieldSz = (int) Math.round(mState.mModel.getAttackPower() * (mState.mModel.multSavageDefense * mState.mModel.getSavageDefenseMastery()));
			mCurrentShield = shieldSz;

			if (mState.mIsLogging) mState.mLog.log("Savage Defense: Size: %d\n", shieldSz);

			mNrSavageDefenseProcs++;
			mTotalAbsorb += shieldSz;
			schedule(mState.mModel.savageDefenseBuffDuration);
		}
	}

	public int onDamage(int damage) {
		mNrPossibleAbsorbs++;
		if (isUp()) {
			int absorbed = Math.min(mCurrentShield, damage);

//			if (mState.mIsLogging) mState.mLog.log("Savage Defense Absorb: Absorbed: %d Remaining: %d\n", absorbed, mCurrentShield - absorbed);

			mCurrentShield -= absorbed;
			mDamageAbsorbed += absorbed;
			if (mCurrentShield == 0) cancel();
			if (absorbed > 0) mNrAbsorbs++;

			return damage - absorbed;
 		}

		return damage;
	}

	long mTotalAbsorb;
	long mDamageAbsorbed;
	int mCurrentShield;
	int mNrSavageDefenseProcs;
	int mNrPossibleAbsorbs;
	int mNrAbsorbs;
}
